/**
 * 
 */
package com.ewing.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.axis.utils.StringUtils;
import org.apache.log4j.Logger;

import com.ewing.busi.seller.service.SellerShopService;
import com.ewing.busi.system.model.SysParam;
import com.ewing.busi.web.model.WebBlockVar;
import com.ewing.busi.web.service.WebBlockService;
import com.ewing.busi.web.service.WebBlockVarService;
import com.ewing.common.constant.SysParamCode;
import com.ewing.common.exception.ShopException;
import com.ewing.common.exception.WebBlockException;
import com.ewing.core.app.action.base.BaseAction;
import com.ewing.core.app.action.base.ResponseData;
import com.ewing.core.app.action.base.ResponseUtils;
import com.ewing.core.jdbc.util.PageBean;

/**
 * @author tanson
 * 
 */
public class WebBlockVarAction extends BaseAction {
	private static Logger logger = Logger.getLogger(MainAction.class);
	private static final String LIST_PAGE = "/admin/blockvar/varlist.html";
	private static final String EDIT_FORM = "/admin/blockvar/varedit.html";
	@Resource
	private WebBlockService webBlockService;
	@Resource
	private WebBlockVarService webBlockVarService;
	@Resource
	private SellerShopService sellerShopService;

	/**
	 * 查詢列表
	 */
	public void show() {
		try {
			Map<String, Object> dataModel = new HashMap<String, Object>();
			String pageStr = request.getParameter("page");
			String pageSizeStr = request.getParameter("pageSize");
			Integer webBlockId = getIntegerParameter("webBlockId");
			Integer page = StringUtils.isEmpty(pageStr) ? null : Integer
					.valueOf(pageStr);
			Integer pageSize = StringUtils.isEmpty(pageSizeStr) ? null
					: Integer.valueOf(pageSizeStr);
			String condition = "shop_id="
					+ sellerShopService.checkAndReturnShopId(getLoginUserId())
					+ " and web_block_id=" + webBlockId + bulidConditionSql();
			PageBean<WebBlockVar> pageBean = baseModelService.pageQuery(condition,
					bulidOrderBySql(), pageSize, page, WebBlockVar.class);
			pageBean.setPageUrl(getPaginationUrl("/Admin-WebBlockVar-show.action"));
			List<SysParam> iseffCode = sysParamService
					.getSysParamByRoot(SysParamCode.ISEFF);
			dataModel.put("iseffCode", iseffCode);
			dataModel.put("blockVarPageBean", pageBean);
			render(LIST_PAGE, dataModel);
		} catch (ShopException e) {
			renderAdminFail(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 显示编辑表单
	 */
	public void showEditForm() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		try {
			Integer id = getIntegerParameter("id");
			Integer webBlockId = getIntegerParameter("webBlockId");
			if (id != null) {
				webBlockVarService.checkBlockVar(sellerShopService
						.checkAndReturnShopId(getLoginUserId()), id);
				WebBlockVar webBlockVar = webBlockVarService.findBlockVar(
						sellerShopService
								.checkAndReturnShopId(getLoginUserId()), id);
				dataModel.put("blockVarBean", webBlockVar);

			}
			dataModel.put("webBlockId", webBlockId);
			List<SysParam> iseffCode = sysParamService
					.getSysParamByRoot(SysParamCode.ISEFF);
			dataModel.put("iseffCode", iseffCode);
			render(EDIT_FORM, dataModel);
		} catch (WebBlockException e) {
			renderAdminFail(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 删除
	 */
	public void delete() {
		ResponseData responseData = null;
		try {
			String selectItems = request.getParameter("selectItems");
			if (selectItems.isEmpty()) {
				responseData = ResponseUtils.fail("没有选中的数据！");
				this.outResult(responseData);
				return;
			}
			String[] selectArr = selectItems.split(",");
			for (String id : selectArr) {
				webBlockVarService.checkBlockVar(sellerShopService
						.checkAndReturnShopId(getLoginUserId()), Integer
						.valueOf(id));
				WebBlockVar webBlockVar = webBlockVarService.findBlockVar(
						sellerShopService
								.checkAndReturnShopId(getLoginUserId()),
						Integer.valueOf(id));

				if (webBlockVar != null)
					baseModelService.delete(webBlockVar);
			}
			responseData = ResponseUtils.success("删除成功！");
		} catch (WebBlockException e) {
			responseData = ResponseUtils.fail("删除失败！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseData = ResponseUtils.fail("删除失败！");
		}
		this.outResult(responseData);
	}

	/**
	 * 保存截图
	 */
	public void save() {
		ResponseData responseData = null;
		try {
			String id = request.getParameter("id");
			String webBlockId = request.getParameter("webBlockId");
			WebBlockVar webBlockVar = new WebBlockVar();
			this.buildPageData(webBlockVar);
			webBlockVar.setShopId(sellerShopService
					.checkAndReturnShopId(getLoginUserId()));
			if (!StringUtils.isEmpty(id)) {
				webBlockVar.setId(Integer.valueOf(id));
				baseModelService.update(webBlockVar);
			} else {
				baseModelService.save(webBlockVar);
			}
			responseData = ResponseUtils.success("保存成功！");
		}catch (ShopException e) {
			responseData = ResponseUtils.fail("保存失败！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseData = ResponseUtils.fail("保存失败！");
		}
		this.outResult(responseData);
	}

}
